package com.chuzhong.system.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chuzhong.common.enums.AttachType;
import com.chuzhong.common.enums.FileType;
import com.chuzhong.common.utils.DateUtils;
import com.chuzhong.system.domain.SysAttachments;
import com.chuzhong.system.mapper.SysAttachmentsMapper;
import com.chuzhong.system.service.ISysAttachmentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

/**
 * 系统附件 附件Service业务层处理
 *
 * @author chuzhong
 * @date 2021-03-15
 */
@Service
public class SysAttachmentsServiceImpl implements ISysAttachmentsService
{
    @Autowired
    private SysAttachmentsMapper sysAttachmentsMapper;

    @Autowired
    private HttpServletRequest request;

    /**
     * 查询系统附件 附件
     *
     * @param id 系统附件 附件ID
     * @return 系统附件 附件
     */
    @Override
    public SysAttachments selectSysAttachmentsById(Long id)
    {
        return sysAttachmentsMapper.selectSysAttachmentsById(id);
    }

    /**
     * 查询系统附件 附件列表
     *
     * @param sysAttachments 系统附件 附件
     * @return 系统附件 附件
     */
    @Override
    public List<SysAttachments> selectSysAttachmentsList(SysAttachments sysAttachments)
    {
        return sysAttachmentsMapper.selectSysAttachmentsList(sysAttachments);
    }

    /**
     * 新增系统附件 附件
     *
     * @param sysAttachments 系统附件 附件
     * @return 结果
     */
    @Override
    public int insertSysAttachments(SysAttachments sysAttachments)
    {
    sysAttachments.setCreatedTime(DateUtils.getNowDate());
        return sysAttachmentsMapper.insertSysAttachments(sysAttachments);
    }

    /**
     * 修改系统附件 附件
     *
     * @param sysAttachments 系统附件 附件
     * @return 结果
     */
    @Override
    public int updateSysAttachments(SysAttachments sysAttachments)
    {
        sysAttachments.setUpdatedTime(DateUtils.getNowDate());
        return sysAttachmentsMapper.updateSysAttachments(sysAttachments);
    }

    /**
     * 批量删除系统附件 附件
     *
     * @param ids 需要删除的系统附件 附件ID
     * @return 结果
     */
    @Override
    public int deleteSysAttachmentsByIds(Long[] ids)
    {
        return sysAttachmentsMapper.deleteSysAttachmentsByIds(ids);
    }

    /**
     * 删除系统附件 附件信息
     *
     * @param id 系统附件 附件ID
     * @return 结果
     */
    @Override
    public int deleteSysAttachmentsById(Long id)
    {
        return sysAttachmentsMapper.deleteSysAttachmentsById(id);
    }

    /**
     * @Description 附件保存
     * @author ChuZhong
     * @param relId      业务id
     * @param attachType 业务类型
     * @param fileType   文件类型
     * @return 条数
     * @date 2021/3/16
     */
    @Transactional
    public int saveFileListByRequestBody(Long relId, AttachType attachType, FileType fileType,JSONArray fileList){
        deleteSysAttachmentByRelId(relId, attachType, fileType);
        Integer i = 0;

        if(fileList != null && !fileList.isEmpty()){
            for(Object file: fileList){
                JSONObject json = (JSONObject) JSONObject.toJSON(file);
                SysAttachments attachment = new SysAttachments();
                attachment.setRelId(relId);
                attachment.setAttachType(attachType.name());
                attachment.setFileType(fileType.name());
                attachment.setFileName(json.getString("name"));
                attachment.setFilePath(json.getString("url"));
                int i1 = insertSysAttachments(attachment);
                i += i1;
            }
        }
        return i;
    }
    public int deleteSysAttachmentByRelId(Long relId, AttachType attachType, FileType fileType){
        String fileTypeStr = fileType != null ? fileType.name() : null;
        return sysAttachmentsMapper.deleteSysAttachmentByRelId(relId, attachType.name(), fileTypeStr);
    }
}
